/*
 *  Copyright 2020-2025 the original author or authors.
 *  You cannot use this file unless authorized by the author.
 */

package org.ipig.web.context;

import org.ipig.commons.concurrent.ThreadLocalNamed;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * HttpWebContext
 *
 * @author <a href="mailto:comchnts@163.com">chinats</a>
 * @version $$Id$$
 * @since 1.0
 */
public abstract class HttpWebContext {
    private static final ThreadLocal<HttpServletRequest> requestHolder = new ThreadLocalNamed("http 请求会话");
    private static final ThreadLocal<HttpServletResponse> responseHolder = new ThreadLocalNamed("http 响应会话");


    public static HttpServletResponse getResponse() {
        return (HttpServletResponse) responseHolder.get();
    }


    public static void setResponse(HttpServletResponse response) {
        if (response == null) {
            responseHolder.remove();
        }
        responseHolder.set(response);
    }


    public static void setRequest(HttpServletRequest request) {
        if (request == null) {
            requestHolder.remove();
        }
        requestHolder.set(request);
    }


    public static HttpServletRequest getRequest() {
        return (HttpServletRequest) requestHolder.get();
    }
}